package com;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Sai
 * Created by Sai on 2019-03-15.
 *
 *
 * @JsonbProperty - 用于指定自定义字段名称
 * @JsonbTransient - 当我们想要在反序列化/序列化期间忽略该字段时
 * @JsonbDateFormat - 当我们想要定义日期的显示格式时
 * @JsonbNumberFormat - 用于指定数值的显示格式
 * @JsonbNillable - 用于启用空值的序列化
 */

public class RegisterDTO {
    private String userid;
    private String email;
    private String password;
    private String confirm;

    private Map<String,String> errors=null;

    public RegisterDTO() {
        this.userid="";
        this.email="";
        this.password="";
        this.confirm="";
        this.errors=new HashMap<String, String>();
    }

    public boolean isVaildate() throws IOException {
        boolean flag=true;
        if(!this.userid.matches("\\w{6,15}")){//用户名长度为6-15
            flag=false;
            this.userid="";
            this.errors.put("errname","用户名需要为6-15位字母或数字");
        }
        if(!this.email.matches("\\w+@\\w+\\.\\w+\\.?\\w*")){
            flag=false;
            this.email="";
            this.errors.put("erremail","输入email地址错误");
        }
        if(!this.password.matches("\\w{6,18}")){
            flag=false;
            this.password="";
            this.errors.put("errpwd","密码需要6-18位字母或数字");
        }
        if(!this.confirm.equals(this.password)){
            flag=false;
            this.confirm="";
            this.errors.put("errconfirm","密码与确认密码不符");
        }

        //注册信息合法
        if(flag){
            //将注册信息写入文本文件中

            //检查用户名是否已存在
            //是本地账号还是绑定账号


            //先确认是否已有账号

            boolean isreg=false;
            boolean islocal=false;
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream("Userpwd.txt")));
            while(bufferedReader.ready()){
                String str=bufferedReader.readLine();
                String[] parts=str.split(",");

                if(parts[0].equals(userid)){
                    //用户名存在
                    isreg=true;
                    //检查是否为本账号
                    if(parts[1].equals("123456"))// 绑定账号
                        islocal=false;
                    else//本地用户
                        islocal=true;
                }
            }


            //存在本地用户,返回，报错
            //不存在本地用户，存在绑定用户,新增本地用户信息，不改动用户信息
            //本地和绑定用户都不存在,新增本地用户信息，新增用户信息

            //存在本地用户
            if(isreg&&islocal){
                //写入错误信息
                this.userid="";
                flag=false;
                this.errors.put("erradduser","用户名已被占用，请更换");
            }

            //不存在本地用户，存在绑定用户
            if(isreg&&!islocal){
                File pwd = new File("Userpwd.txt");
                File pinfo = new File("User.txt");

                //写入账号密码
                OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(pwd, true), "UTF-8");
                writer.append(userid + ",");
                writer.append(email + ",");
                writer.append(password + "\n");
                writer.close();
            }

            //本地和绑定用户都不存在
            if(!isreg){
                try {
                    File pwd = new File("Userpwd.txt");
                    File pinfo = new File("User.txt");

                    //写入账号密码
                    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(pwd, true), "UTF-8");
                    writer.append(userid + ",");
                    writer.append(email + ",");
                    writer.append(password + "\n");
                    writer.close();

                    //写入默认信息
                    OutputStreamWriter pinfowriter = new OutputStreamWriter(new FileOutputStream(pinfo, true), "UTF-8");
                    pinfowriter.append(userid + ",");
                    pinfowriter.append(userid + ",");
                    pinfowriter.append("未绑定" + ",");
                    pinfowriter.append("未绑定" + ",");
                    pinfowriter.append("未绑定" + ",");
                    pinfowriter.append("未绑定" + ",");
                    pinfowriter.append("未绑定" + ",");
                    pinfowriter.append("未绑定" + "\n");

                    pinfowriter.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
        return flag;
    }

    public String getErrMsg(String key){
        String value=this.errors.get(key);
        //若无错误信息，返回空值
        return value==null?"":value;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public Map<String, String> getErrors() {
        return errors;
    }

    public void setErrors(Map<String, String> errors) {
        this.errors = errors;
    }

}